﻿using UnityEngine;
namespace UGUIMan
{
    public class Eye:MonoBehaviour
    {
        bool cursor=false;
        private Rect range {
            get { return transform.parent.GetComponent<RectTransform>().rect; }
            set
            {
                
            }
        }
        private Rect eyerect
        {
            get
            {
                return GetComponent<RectTransform>().rect;
            }
            set
            {
                transform.GetComponent<RectTransform>().SetPositionAndRotation(new Vector3(value.x, value.y, transform.position.z), Quaternion.identity);
            }
        }
        
        public Vector3 mousepos;
        public Vector3 pos;
        public Vector3 parentpos;
        public Vector2 scrollpos=new Vector2();
        
        public Rect r;
        public Rect er;
        Vector2 current;
        bool up=true;
        bool down=true;
        bool left=true;
        bool right=true;

        float speed = 1;

        public float deltaspeed = 1;

        private void Start()
        {
            pos = transform.position;
            parentpos = transform.parent.position;
        }
        Vector3 currentpos =new Vector3();

        Vector3 lastpos = new Vector3();
        private void OnGUI()
        {
            
            if (lastpos == default(Vector3))
            {
                lastpos = Input.mousePosition;
            }
            currentpos = Input.mousePosition;
            Vector3 dpos = ( currentpos - lastpos ).normalized;
            
            scrollpos = new Vector2(dpos.x, dpos.y);
            scrollpos = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
            if (Mathf.Abs(scrollpos.x) > 0)
            {
                speed += Time.deltaTime * deltaspeed;
                speed = Mathf.Clamp(speed, 1, 2);
            }
            else
            {
                speed -= Time.deltaTime;
                speed = Mathf.Clamp(speed, 1, 2);
            }
            var e0 = GameObject.Find("Eye0");
            if (transform.Equals(e0.transform))
            {

            }
            else
            {
                transform.localPosition = Vector3.Lerp(transform.localPosition, e0.transform.localPosition,10);
            }
            transform.localPosition += new Vector3(scrollpos.x, scrollpos.y, 0) * 3 * speed;
            lastpos = currentpos;
        }
        
        private void Update()
        {
            var deltapos = transform.localPosition;
            pos = deltapos;
            r = transform.parent.GetComponent<RectTransform>().rect;
            deltapos = new Vector3(Mathf.Clamp(deltapos.x, r.x, r.x + r.width), Mathf.Clamp(deltapos.y, r.y, r.y + r.height),deltapos.z);
            
            transform.localPosition = deltapos;
            er = new Rect(deltapos.x + r.x, deltapos.y + r.y, r.width, r.height);
            if (!cursor)
            {
                Cursor.visible = false;
                Cursor.lockState = CursorLockMode.Confined;
            }
            else
            {
                Cursor.visible = true;
                Cursor.lockState = CursorLockMode.None;
            }
            if (Input.GetKeyDown(KeyCode.M))
            {
                cursor = !cursor;
            }
        }
    }
}